﻿<%@ Page Title="" Language="C#" MasterPageFile="~/NonExt.Master" AutoEventWireup="true" CodeBehind="r_cost_revenues.aspx.cs" Inherits="BSC.report.r_cost_revenues" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script>
    function CreateDate()
    {
        var curYear=<%= DateTime.Now.ToString("yyyy") %>;
        var curMonth=<%= DateTime.Now.ToString("MM") %>;
        var html="";
        var selected=""
        for(var i=1;i<=12;i++)
        {
            if(i==curMonth)selected="selected";else selected="";
            if(i<10)
                html+='<option value="0'+i+'" '+selected+'>Tháng '+i+'</option>';
            else
                html+='<option value="'+i+'" '+selected+'>Tháng '+i+'</option>';
        }
        $(monthFrom).html(html);
        $(monthTo).html(html);
        html='';
        for(var i=curYear-5;i<=curYear+5;i++)
        {
            if(i==curYear)selected="selected";else selected="";
            html+='<option value="'+i+'" '+selected+'>Năm '+i+'</option>';
        }
        $(yearFrom).html(html);
        $(yearTo).html(html);
    }
    $(document).ready(function(){
//        $( "#from" ).datepicker({ dateFormat: 'yymm' });
//        $( "#to" ).datepicker({ dateFormat: 'yymm' });
        CreateDate();
        $('#wrapper').width($(document).width()-20);
        $('#wrapper').height($(document).height()-20);
        $('#wrapper').css('overflow','scroll-x')
        $('.export').click(function(){
            $("body").mask("Đang xử lý dữ liệu...");
            $.ajax({
                type: "POST",
                url: "r_cost_revenues.aspx/ExportData",
                data: '{}',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(response){
                    //data=JSON.parse(response.d);
                    
                    var str=response.d+"";
                    //console.log(data);
                    if(str!="")
                    {
                        $("body").unmask();
                        window.location=str;
                    }
                    else
                    {
                        $("body").unmask();
                        alert("Có lỗi trong quá trình xử lý");
                    }
                },
                failure: function(response) {
                    $("body").unmask();
                    alert("Có lỗi trong quá trình xử lý");
                }
            });
        });
        $("#getdata").click(function(){
            $("body").mask("Đang xử lý dữ liệu...");
             $.ajax({
                type: "POST",
                url: "r_cost_revenues.aspx/GetCost",
                data: "{'from':'"+$("#yearFrom").val()+$("#monthFrom").val()+"','to':'"+$("#yearTo").val()+$("#monthTo").val()+"'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: OnSuccess,
                failure: function(response) {
                    console.log(response)
                }
            });
            $("body").unmask();
        });
    });
//    $.ajax({
//        type: "POST",
//        url: "r_cost_revenues.aspx/GetCost",
//        data: '{}',
//        dataType: "json",
//        contentType: "application/json; charset=utf-8",
//        success: OnSuccess,
//        failure: function(response) {
//            console.log(response)
//        }
//    });
    
    function OnSuccess(response) {
        var data = JSON.parse(response.d);        
        $('#pivot .frame').remove();
        $('#pivot').html('<div class="frame"></div>');
        $('#pivot .frame').jqpivot({
            data: data,
            dimensions: {
                "timekey": {name:"timekey", sortIndex: 1, type: 'number',label:"Time key"},
                "month": {name:"month", sortIndex: 1, type: 'undefined',label:"Tháng" },
                "cost_code": { name:"cost_code",sortIndex: 1, type: 'string',label:"STT" },
                "criteria_name": {name:"criteria_name", sortIndex: 1, type: 'string',label:"Tiêu chí" },
                "unit_name": { name:"unit_name",sortIndex: 1, type: 'string',label:"ĐVT" },
                "branch_code": {name:"branch_code", sortIndex: 1, type: 'string',label:"Mã khu vực" },
                "branch_name": {name:"branch_name", sortIndex: 1, type: 'string',label:"Khu vực" },
                "cost": { name:"cost",sortIndex: 1, type: 'number',label:"Giá trị" },
                "accumulate": {name:"accumulate", sortIndex: 1, type: 'number',label:"Lũy kế" },
                "type": { name:"type",sortIndex: 1, type: 'number',label:"Loại" },  
                "unit_code": {name:"unit_code", sortIndex: 1, type: 'number',label:"Mã ĐVT" }, 
            },
            columns: ["branch_code","branch_name"],
            rows: ["month", "cost_code", "criteria_name", "unit_name"],
            facts: [{ by: "cost", func: "sum" }, { by: "accumulate", func: "sum"}],
            showColTotal:false,
            showRowTotal:true,
            
        });
        
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="content" runat="server">
<div id="wrapper">
    <div class="toolBar img-button">
        <ul>
            <li class="search">
                
                <label>Từ ngày:</label>
                <select id="yearFrom">
                    <option></option>
                </select>
                <select id="monthFrom">
                    <option></option>
                </select>
                <label>Đến ngày</label>
                <select id="yearTo">
                    <option></option>
                </select>
                <select id="monthTo">
                    <option></option>
                </select>
                <button id="getdata" class="img-button ui-button ui-state-default ui-corner-all ui-button-text-only">
                    <img src="/content/images/refresh.png" class="icon" />
                    <span class="ui-button-text">
                        Tải dữ liệu
                    </span>
                </button>
            </li>
            
            <li class="export">
                <button class="img-button ui-button ui-state-default ui-corner-all ui-button-text-only">
                    <img src="/content/images/xl.png" class="icon" />
                    <span class="ui-button-text">
                        Export
                    </span>
                </button>
            </li>
        </ul>
    </div>
    
    <div id="pivot">
        <div class="frame">
        </div>    
    </div>
</div>
</asp:Content>
